简体   繁体   English

MediaPlayer重置和释放之间的区别

[英]MediaPlayer difference between reset and release

I am having two buttons and two songs. 我有两个按钮和两首歌。 Button 1 initializes and plays song 1. Same with button 2 and song 2. 按钮1初始化并播放歌曲1.与按钮2和歌曲2相同。

So, on click, button 1 uses create , setDataSource , prepare and start method . 因此,在单击时,按钮1使用createsetDataSourcepreparestart method So, what's the difference between the reset and the release method? 那么,重置和释放方法有什么区别?

Should button 2 use reset , create , setDataSource , prepare and start 按钮2应该使用resetcreatesetDataSourcepreparestart

OR release , create , setDataSource , prepare and then start ? OR releasecreatesetDataSourceprepare然后start

For my testings, it's the exact same... 对于我的测试,它完全相同......

From the API docs (which I strongly recommend you read): API文档 (我强烈建议您阅读):

release(): 发布():

Releases resources associated with this MediaPlayer object. 释放与此MediaPlayer对象关联的资源。 It is considered good practice to call this method when you're done using the MediaPlayer. 完成使用MediaPlayer后调用此方法被认为是一种好习惯。 In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. 特别是,每当应用程序的Activity暂停(调用其onPause()方法)或停止(调用其onStop()方法)时,应调用此方法以释放MediaPlayer对象,除非应用程序具有特殊功能需要保持对象周围。 In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. 除了持有不必要的资源(例如内存和编解码器实例)之外,如果不再需要MediaPlayer对象,则无法立即调用此方法也可能导致移动设备的电池持续消耗,如果没有则会导致其他应用程序的播放失败设备支持相同编解码器的多个实例。 Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time. 即使支持相同编解码器的多个实例,当同时使用不必要的多个实例时,也可能预期性能下降。

reset(): 重启():

Resets the MediaPlayer to its uninitialized state. 将MediaPlayer重置为未初始化状态。 After calling this method, you will have to initialize it again by setting the data source and calling prepare(). 调用此方法后,您必须通过设置数据源并调用prepare()再次初始化它。

So based on that you should be called reset() rather than release() as you still require the object after a song has played. 因此,基于此,您应该调用reset()而不是release()因为您在播放歌曲后仍然需要该对象。

If you use reset() your object is like " just created ", and if you use release() the object is deleted(no longer available). 如果使用reset()对象就像“ 刚刚创建 ”,如果使用release() ,则删除对象(不再可用)。 I guess if you want to play the song more than once, you should use reset() . 我想如果你想多次play这首歌,你应该使用reset()

If you have a list of songs with their buttons you should use release() . 如果你有一个带按钮的歌曲列表,你应该使用release()

If someone clicked on the button and the song has ended, if she wanted to start the song again! 如果有人点击按钮并且歌曲已经结束,如果她想再次开始播放歌曲! in this case you use reset() and initialize the object again. 在这种情况下,您使用reset()并再次初始化对象。

When you destroy the activity you should call release() method. 当您销毁活动时,您应该调用release()方法。

public synchronized static MediaPlayer getWhistlePlayer(Context context) {
    if (whistlePlayer == null) {
        whistlePlayer = MediaPlayer.create(context.getApplicationContext(), R.raw.whistle);
    } else {
        whistlePlayer.reset();
        whistlePlayer = MediaPlayer.create(context.getApplicationContext(), R.raw.whistle);
    }
    whistlePlayer.start();
    return whistlePlayer;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM