我在使用以下代码时遇到问题。我在我的 MediaController 上调用了 setPrevNextListener 并为 Prev 和 Next 定义了两个 onClickListeners。当我单击下一个按钮而不是前进一首轨道时,我前进了两首轨道。这似乎是由于以某种方式调用了 onCompletion。 MediaPlayer.reset() 是否调用 onCompletion?我包含了 logcat 输出和我的代码。如果我做错了什么,请告诉我。提前致谢。
日志:
02-24 00:36:34.826: D/MP(6675): Next Button Clicked, index was: 0
02-24 00:36:34.837: D/MP(6675): About to call Reset()
02-24 00:36:34.906: D/MP(6675): Inside setUpPlayer
02-24 00:36:34.917: D/MP(6675): Called setDataSource with index: 1
02-24 00:36:34.917: D/MP(6675): Leaving setUpPlayer
02-24 00:36:34.917: D/MP(6675): About to call prepareAsync()
02-24 00:36:34.937: D/MP(6675): Leaving next button
02-24 00:36:35.226: E/MediaPlayer(6675): Attempt to call getDuration without a valid mediaplayer
02-24 00:36:35.226: E/MediaPlayer(6675): error (-38, 0)
02-24 00:36:35.276: E/MediaPlayer(6675): Error (-38,0)
02-24 00:36:35.287: D/MP(6675): Inside onCompletion
02-24 00:36:35.337: D/MP(6675): About to call Reset()
02-24 00:36:35.347: D/MP(6675): Inside setUpPlayer
02-24 00:36:35.356: D/MP(6675): Called setDataSource with index: 2
02-24 00:36:35.356: D/MP(6675): Leaving setUpPlayer
02-24 00:36:35.356: D/MP(6675): About to call prepareAsync()
02-24 00:36:35.377: D/MP(6675): Leaving onCompletion
02-24 00:36:36.517: D/MP(6675): Inside onPrepared, index is: 2
02-24 00:36:36.577: D/MP(6675): Leaving onPrepared
代码:
public class MusicPlayerActivity extends Activity implements OnPreparedListener, OnCompletionListener, MediaController.MediaPlayerControl {
private MediaPlayer mp;
private MediaController mc;
private SonarDatabase db;
private SubsonicAPIConnector sonic;
ArrayList<String> songs;
int index = 0;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player);
// Get params from intent
Intent i = getIntent();
String albumId = i.getStringExtra("albumId");
String artistId = i.getStringExtra("artistId");
String albumName = i.getStringExtra("albumName");
String songName = i.getStringExtra("songName");
String songId = i.getStringExtra("songId");
String coverArt = "al-"+albumId+".jpeg";
String duration = i.getStringExtra("duration");
Log.d("MP", "results: albumName:" + albumName + ", songName: " + songName + ", songId: "
+ songId + ", duration: " + duration + ", coverArt: " + coverArt + ", artistId: "
+ artistId + ", albumId: " + albumId);
db = new SonarDatabase(getApplicationContext());
sonic = new SubsonicAPIConnector(db.getURL(), db.getUsername(), db.getPassword());
String artistName = db.getArtistNameById(artistId);
setTitle("Now Playing");
songs = db.getSongListForAlbum(albumId);
index = songs.indexOf(songId);
Log.d("MP", "songid: " + songId + ", index: " + index);
// Update text views with song information
TextView txtArtist = (TextView) findViewById(R.id.txtArtistName);
txtArtist.setText(artistName);
TextView txtAlbum = (TextView) findViewById(R.id.txtAlbumName);
txtAlbum.setText(albumName);
TextView txtSong = (TextView) findViewById(R.id.txtSongName);
txtSong.setText(songName);
// Show the album art
File img = new File(this.getFilesDir()+"/covers/"+coverArt);
if(img.exists()) {
Log.d("MP", "Found image at: " + img.toString());
ImageView imgView = (ImageView) findViewById(R.id.cover_art);
imgView.setImageBitmap(BitmapFactory.decodeFile(img.getPath()));
} else {
Log.d("MP", "Couldn't find image: " + img.toString());
}
// Create the media player and controller
mp = new MediaPlayer();
mp.setOnPreparedListener(this);
mp.setOnCompletionListener(this);
mc = new NonHidingMediaController(this);
mc.setPrevNextListeners(new View.OnClickListener() {
@Override
public void onClick(View v) {
// next button clicked
Log.d("MP", "Next Button Clicked, index was: " + index);
playNextTrack();
Log.d("MP", "Leaving next button");
}
}, new View.OnClickListener() {
@Override
public void onClick(View v) {
// previous button clicked
Log.d("MP", "Previous button clicked, index was: " + index);
if(mp.isPlaying() && mp.getCurrentPosition() < 2000 && index != 0) {
Log.d("MP", "Start the prior song.");
playPreviousTrack();
} else {
Log.d("MP", "Restart this song.");
restartCurrentTrack();
}
Log.d("MP", "Leaving previous button");
}
});
mc.setMediaPlayer(this);
setUpPlayer();
Log.d("MP", "About to call prepareAsync()");
mp.prepareAsync();
}
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("MP", "Inside onCompletion");
//TODO add code for scrobbling here.
playNextTrack();
Log.d("MP", "Leaving onCompletion");
}
private void playNextTrack() {
index++;
TextView txtSong = (TextView) findViewById(R.id.txtSongName);
txtSong.setText(db.getSongNameById(songs.get(index)));
Log.d("MP", "About to call Reset()");
mp.reset();
setUpPlayer();
Log.d("MP", "About to call prepareAsync()");
mp.prepareAsync();
}
private void playPreviousTrack() {
index--;
TextView txtSong = (TextView) findViewById(R.id.txtSongName);
txtSong.setText(db.getSongNameById(songs.get(index)));
Log.d("MP", "About to call Reset()");
mp.reset();
setUpPlayer();
Log.d("MP", "About to call prepareAsync()");
mp.prepareAsync();
}
private void restartCurrentTrack() {
mp.seekTo(0);
}
public void onPrepared(MediaPlayer mp) {
Log.d("MP", "Inside onPrepared, index is: " + index);
mc.setMediaPlayer(this);
mc.setAnchorView(findViewById(R.id.music_player_view));
mp.start();
handler.post(new Runnable() {
public void run() {
mc.setEnabled(true);
mc.show();
}
});
Log.d("MP", "Leaving onPrepared");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_music_player, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("MP", "OnTouchEvent called");
mc.show();
return false;
}
private void setUpPlayer() {
Log.d("MP", "Inside setUpPlayer");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.setDataSource(sonic.streamString("&id=" + songs.get(index)));
Log.d("MP", "Called setDataSource with index: " + index);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("MP", "Leaving setUpPlayer");
}
@Override
protected void onStop() {
super.onStop();
mc.hide();
mp.stop();
mp.release();
}
public void start() {
mp.start();
}
public void pause() {
mp.pause();
}
public int getDuration() {
return mp.getDuration();
}
public int getCurrentPosition() {
return mp.getCurrentPosition();
}
public void seekTo(int i) {
mp.seekTo(i);
}
public boolean isPlaying() {
return mp.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
public class NonHidingMediaController extends MediaController {
public NonHidingMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonHidingMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
public NonHidingMediaController(Context context) {
super(context);
}
@Override
public void show(int timeout) {
super.show(0);
}
}
}
最佳答案
我深入研究了 android 源代码,发现任何未处理的错误都会导致 MediaPlayer 调用 onCompletion 处理程序。在这种情况下,您似乎是在空闲、已初始化或错误状态下调用 getDuration。可能从媒体 Controller 内部。我建议在任何调用 getDuration 的地方之前添加一些 Log.d() 以查看导致问题的调用位置。
关于Android MediaPlayer.reset() 正在调用 onCompletion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15049097/
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
如何找到调用此方法的位置?defto_xml(options={})binding.pryoptions=options.to_hifoptions&&options.respond_to?(:to_h)serializable_hash(options).to_xml(options)end 最佳答案 键入caller。这将返回当前调用堆栈。文档:Kernel#caller.例子[0]%rspecspec10/16|===================================================62=====
Rails相对较新。我正在尝试调用一个API,它应该向我返回一个唯一的URL。我的应用程序中捆绑了HTTParty。我已经创建了一个UniqueNumberController,并且我已经阅读了几个HTTParty指南,直到我想要什么,但也许我只是有点迷路,真的不知道该怎么做。基本上,我需要做的就是调用API,获取它返回的URL,然后将该URL插入到用户的数据库中。谁能给我指出正确的方向或与我分享一些代码? 最佳答案 假设API为JSON格式并返回如下数据:{"url":"http://example.com/unique-url"
我正在写一篇关于在Ruby中几乎一切都是对象的博客文章,我试图通过以下示例来展示这一点:classCoolBeansattr_accessor:beansdefinitialize@bean=[]enddefcount_beans@beans.countendend所以从类中我们可以看出它有4个方法(当然,除非我错了):它可以在创建新实例时初始化一个默认的空bean数组它可以计算它有多少个bean它可以读取它有多少个bean(通过attr_accessor)它可以向空数组写入(或添加)更多bean(也通过attr_accessor)但是,当我询问类本身它有哪些实例方法时,我没有看到默认
我写了一个非常简单的rake任务来尝试找到这个问题的根源。namespace:foodotaskbar::environmentdoputs'RUNNING'endend当在控制台中执行rakefoo:bar时,输出为:RUNNINGRUNNING当我执行任何rake任务时会发生这种情况。有没有人遇到过这样的事情?编辑上面的rake任务就是写在那个.rake文件中的所有内容。这是当前正在使用的Rakefile。requireFile.expand_path('../config/application',__FILE__)OurApp::Application.load_tasks这里