gpt4 book ai didi

java - Android Studio 中基本音乐播放器中的音频问题

转载 作者:行者123 更新时间:2023-12-01 16:54:53 24 4
gpt4 key购买 nike

我在 Android Studio 中创建了一个基本的音乐播放器应用程序,音乐文件存储在资源下的 raw 目录中,格式为 Mp3。问题是,虽然事件监听器内的 toast 被显示,但音频只播放一次(我真的不知道如何)。代码如下。

list 文件

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

<uses-permission android:name="android.permission.INTERNET" />

<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>
</application>

</manifest>

xml 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/greetings"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="221dp"
android:layout_height="298dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="104dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.505"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@android:drawable/ic_media_play" />

<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="@string/pause"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/stop"
app:layout_constraintHorizontal_bias="0.565"
app:layout_constraintStart_toEndOf="@+id/play"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.205" />

<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="@string/play"
app:layout_constraintBaseline_toBaselineOf="@+id/pause"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="@string/stop"
app:layout_constraintBaseline_toBaselineOf="@+id/pause"
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

java代码

package com.example.basicmusicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
MediaPlayer player;

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

/*
an object of class MediaPlayer created named player
a mp3 file is added to the object using create method*/
player = MediaPlayer.create(this, R.raw.adele);

// setting up inline event listeners to the button
// first comes the play button
Button play_music = findViewById(R.id.play);
play_music.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Playing sound",Toast.LENGTH_SHORT).show();
player.start();

}
});

// adding event listener on pause button
Button pause_music = findViewById(R.id.pause);
pause_music.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "paused sound",Toast.LENGTH_SHORT).show();
player.pause();

}
});

// adding event listener for stop button
Button stop_music = findViewById(R.id.stop);
stop_music.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "stopped sound",Toast.LENGTH_SHORT).show();
player.stop();

}
});



}
}

最佳答案

 private MediaPlayer mp;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);

final TextView t = (TextView) findViewById(R.id.textView1);

b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
mp.start();
}

});
}

private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}

关于java - Android Studio 中基本音乐播放器中的音频问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61606642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com