【问题标题】:Background music won't play automatically on App start?App启动时背景音乐不会自动播放?
【发布时间】:2017-03-26 20:48:02
【问题描述】:

如果这个问题之前已经重复了 1000 次,请道歉,但我真的被卡住了,需要帮助:/。

[问题]

QUESTION 1: Where in the coding I need to add or change to make my background music play automatically when app starts? 现在我只能通过使用开始按钮让它播放,它也会在我的其他活动中播放,这也是我想要它做的。

QUESTION 2: If I want more than one music file to be played, what should be implemented?(我知道我需要为此创建一个新问题,但只是想是否可以将这两个问题合并为一个问题,那会更容易)。

.

MusicService.java

public class MyService extends Service {

    MediaPlayer mediaPlayer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mediaPlayer = MediaPlayer.create(this, R.raw.mrkrabs);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        return super.onStartCommand(intent, flags, startId);
    }//onStartCommand ends here


    @Override
    public boolean stopService(Intent name) {
        return super.stopService(name);
    }//stopService ends here


    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
    }//onDestroy ends here


}//MyService ends here

.

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button stopMusic;
    Button startMusic;
    Button nextActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stopMusic = (Button) findViewById(R.id.stopMusic);
        stopMusic.setOnClickListener(this);

        startMusic = (Button) findViewById(R.id.startMusic);
        startMusic.setOnClickListener(this);

        nextActivity = (Button) findViewById(R.id.nextActivity);
        nextActivity.setOnClickListener(this);
    }//onCreate ends here


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.stopMusic:
                stopService(new Intent(this, MyService.class));
                stopMusic.setVisibility(View.GONE);
                startMusic.setVisibility(View.VISIBLE);
                break;

            case R.id.startMusic:
                startService(new Intent(this, MyService.class));
                startMusic.setVisibility(View.GONE);
                stopMusic.setVisibility(View.VISIBLE);
                break;

            case R.id.nextActivity:
                startActivity(new Intent(this, NextActivity.class));
                break;
        }//switch ends here
    }//onClick ends here


}//MainActivity ends here

.

Manifest.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.musicapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" />
        <activity android:name=".NextActivity"></activity>
    </application>

</manifest>

【问题讨论】:

  • 1.只需从应用程序类启动您的服务即可在应用程序启动时播放音乐。 2.你想同时播放所有音乐文件还是顺序播放?
  • @Techierj: 1. 是的,但是如果我更改活动,它将停止播放背景音乐,不是吗? 2. 我确实希望音乐是按顺序播放的。
  • 不,它不会随着活动的变化而停止。对于第二个问题this 会帮助你。
  • 好朋友!这么愚蠢的错误。感谢@Techierj 的帮助。我会看看你发给我的关于顺序播放的链接。希望我是 Android 新手,阅读起来不会有任何困难。
  • yuppp。继续 :) 并且不要忘记支持评论:D

标签: android


【解决方案1】:
  1. 确保您在服务中正确创建媒体播放器、设置源、准备/循环调用。 Refer this

  2. 您可以在您的活动中使用ServiceConnection,并从您的活动中传递和更改来源。这就是典型的音乐应用程序所做的。

Here is the universal music player code you can refer

【讨论】:

  • 感谢朋友的回复,非常感谢。我还有一个问题,如何实现 onPause 让音乐暂停而不是停止和重新启动? @albeee
猜你喜欢
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多