我目前正在开发一个实现Spotify API的Android应用程序.我已经将所有代码连接到我的应用程序以使用教程进行spotify,并且已经在我的应用程序上工作了一段时间.当我在验证用户身份后通过我的应用程序播放歌曲时,它可以很好地工作,就在我的模拟器上.当我将它切换到我的手机时,它无法正常工作,并在android响应中给了我一个INVALID_APP_ID错误.当我卸载Spotify关闭我的手机,然后尝试登录通过我的应用程序spotify,然后我能够从我的手机播放音乐没有任何崩溃.所以我的问题是如何解决这个问题?这是我验证用户的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
// Check if result comes from the correct activity
if (requestCode == requestcode) {
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, intent);
if (response.getType() == AuthenticationResponse.Type.TOKEN) {
Config playerConfig = new Config(this, response.getAccessToken(), client_id);
token = response.getAccessToken();
Spotify.getPlayer(playerConfig, this, new Player.InitializationObserver() {
@Override
public void onInitialized(Player player) {
mPlayer = player;
mPlayer.addConnectionStateCallback(.this);
mPlayer.addPlayerNotificationCallback(.this);
}
@Override
public void onError(Throwable throwable) {
Log.e("MainActivity", "Could not initialize player: " + throwable.getMessage()); …
Run Code Online (Sandbox Code Playgroud) 但是我注意到每当我从另一个方法调用它时,我的桥变量都是nil.我相信这是因为仅在从javascript调用桥接方法时才设置桥.我已经尝试了从创建委托到创建SingleTon类的所有内容.上述工作都没有,我无法弄清楚为什么它仅在我从Javascript调用的方法中可用.这是我的课
Helper.h
#import "RCTBridge.h"
#import "AppDelegate.h"
#import "RCTEventEmitter.h"
@interface Helper : RCTEventEmitter <RCTBridgeModule>
-(void) auth;
@end
Run Code Online (Sandbox Code Playgroud)
这是我的.m文件:Helper.m
#import "AppDelegate.h"
#import "Helper.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
@implementation Helper
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
- (NSArray<NSString *> *)supportedEvents {
return @[@"SpotifyHelper"];
}
RCT_EXPORT_METHOD(auth)
{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"SpotifyHelper" body:@{@"Login": @true}];
printf("Auth");
}
RCT_EXPORT_METHOD(play:(NSString *) uri first: id)
{
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[appDelegate play:uri second:id];
}
@end
Run Code Online (Sandbox Code Playgroud)
我从我的委托内部调用这个方法,如下所示:
[[AppDelegate alloc] init] auth]
Run Code Online (Sandbox Code Playgroud)
这就是我认为它没有初始化的原因.我不知道如何让RCTBridge变量不为零.有帮助吗?