这是我关于Stackoverflow的第一个问题。而且我是法国人,所以请耐心和全面^ _ ^。
在我的项目中,我为播放和录制短信音频信息制作了两个自定义视图:
SimpleAudioPlayer和SimpleAudioRecorder。 SimpleAudioRecorder扩展了SimpleAudioPlayer,因为它也是一个播放器,但具有记录的能力。
我的问题是SimpleAudioRecorder无法在Eclipse预览中实例化,但SimpleAudioPlayer没有问题(抱歉,我没有足够的声誉发布图片......)。
有SimpleAudioPlayer类:
public class SimpleAudioPlayer extends RelativeLayout {
protected ImageButton play;
protected ImageButton stop;
protected MediaPlayer player = null;
public SimpleAudioPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public SimpleAudioPlayer(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SimpleAudioPlayer(Context context) {
super(context);
}
private void init(Context c) {
LayoutInflater li = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.simpleaudioplayer, null);
play = (ImageButton)v.findViewById(R.id.SimpleAudioRecorder_ImageButton_Play);
stop = (ImageButton)v.findViewById(R.id.SimpleAudioRecorder_ImageButton_Stop);
play.setEnabled(false);
stop.setEnabled(false);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startOrPausePlay();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlay();
}
});
addView(v);
}
private void configurePlayer() {
player = new MediaPlayer();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopPlay();
}
});
}
public MediaPlayer getMediaPlayer() {
return this.player;
}
public void openFilename(String filename) {
try {
openFilename(Uri.parse(filename));
} catch (IllegalArgumentException | SecurityException | IllegalStateException e) {
e.printStackTrace();
}
}
/**
* Associe un nom de fichier audio au player.
*
* @param Uri filename le nom du fichier audio à associer
*/
public void openFilename(Uri filename) {
configurePlayer();
try {
player.setDataSource(filename.toString());
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}
public void startOrPausePlay() {
if(player.isPlaying()) {
play.setImageResource(R.drawable.imageboutons_play);
player.pause();
}
else {
stop.setEnabled(true);
play.setImageResource(R.drawable.imageboutons_pause);
try {
player.prepare();
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
player.start();
}
}
public void stopPlay() {
player.pause();
player.seekTo(0);
play.setImageResource(R.drawable.imageboutons_play);
play.setEnabled(true);
stop.setEnabled(false);
}
}
simpleaudioplayer.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SimpleAudioRecorder_LinearLayout_AudioDescPlayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="@+id/SimpleAudioRecorder_ImageButton_Play"
style="@style/translucide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:contentDescription="@string/ProchesRow_Text_ImageButton_Desc_Play"
android:gravity="center_vertical"
android:padding="@dimen/Cursor_col_paddingleft"
android:src="@drawable/imageboutons_play"
android:focusable="false"
android:focusableInTouchMode="false"/>
<ImageButton
android:id="@+id/SimpleAudioRecorder_ImageButton_Stop"
style="@style/translucide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/SimpleAudioRecorder_ImageButton_Play"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@id/SimpleAudioRecorder_ImageButton_Play"
android:contentDescription="@string/ProchesRow_Text_ImageButton_Desc_Stop"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:padding="@dimen/Cursor_col_paddingleft"
android:src="@drawable/imageboutons_stop" />
</RelativeLayout>
现在这是SimpleAudioRecorder类:
public class SimpleAudioRecorder extends SimpleAudioPlayer {
private ImageButton record;
private MediaRecorder recorder = null;
private String OUTPUT_FILE;
private boolean isRecording = false;
public SimpleAudioRecorder(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode())
init(context);
}
public SimpleAudioRecorder(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode())
init(context);
}
public SimpleAudioRecorder(Context context) {
super(context);
}
private void init(Context c) {
LayoutInflater li = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.simpleaudiorecorder, null);
play = (ImageButton)v.findViewById(R.id.SimpleAudioRecorder_ImageButton_Play);
stop = (ImageButton)v.findViewById(R.id.SimpleAudioRecorder_ImageButton_Stop);
record = (ImageButton)v.findViewById(R.id.SimpleAudioRecorder_ImageButton_Record);
play.setEnabled(false);
stop.setEnabled(false);
record.setEnabled(true);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startOrPausePlay();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isRecording)
stopRecord();
else
stopPlay();
}
});
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRecord();
}
});
OUTPUT_FILE = ActivityCommons.PRIVATE_EXTERNAL_STORAGE_FOLDER + ActivityCommons.FOLDER_AUDIO_RECORDED + "audiorecorder.3ggp";
addView(v);
}
public MediaRecorder getMediaRecorder() {
return this.recorder;
}
public Uri getFileRecorded() {
return Uri.parse(OUTPUT_FILE);
}
private void configureRecorder() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
public void startOrPausePlay() {
super.startOrPausePlay();
if(!player.isPlaying())
record.setEnabled(false);
}
public void stopPlay() {
super.stopPlay();
record.setEnabled(true);
}
public void startRecord() {
//Préparation du fichier
File outputFile = new File(OUTPUT_FILE);
if(outputFile.exists())
outputFile.delete();
configureRecorder();
try {
if(outputFile.createNewFile()) { // Si le fichier a bien été créé
recorder.setOutputFile(outputFile.toString()); // on l'associe au recorder
recorder.prepare();
recorder.start();
isRecording = true;
}
} catch (IOException e) {
e.printStackTrace();
}
play.setEnabled(false);
record.setEnabled(false);
stop.setEnabled(true);
}
public void stopRecord() {
recorder.stop();
recorder.release();
isRecording = false;
// On associe directement le fichier enregistrer au player
openFilename(OUTPUT_FILE);
stop.setEnabled(false);
record.setEnabled(true);
play.setEnabled(true);
}
}
最后是simpleaudioplayer.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/SimpleAudioRecorder_LinearLayout_AudioDescPlayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center" >
<ImageButton
android:id="@+id/SimpleAudioRecorder_ImageButton_Play"
style="@style/translucide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:contentDescription="@string/ProchesRow_Text_ImageButton_Desc_Play"
android:gravity="center_vertical"
android:padding="@dimen/Cursor_col_paddingleft"
android:src="@drawable/imageboutons_play"
android:focusable="false"
android:focusableInTouchMode="false"/>
<ImageButton
android:id="@+id/SimpleAudioRecorder_ImageButton_Stop"
style="@style/translucide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/SimpleAudioRecorder_ImageButton_Play"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@id/SimpleAudioRecorder_ImageButton_Play"
android:contentDescription="@string/ProchesRow_Text_ImageButton_Desc_Stop"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:padding="@dimen/Cursor_col_paddingleft"
android:src="@drawable/imageboutons_stop" />
<ImageButton
android:id="@+id/SimpleAudioRecorder_ImageButton_Record"
style="@style/translucide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/SimpleAudioRecorder_ImageButton_Play"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@id/SimpleAudioRecorder_ImageButton_Stop"
android:contentDescription="@string/ProchesRow_Text_ImageButton_Desc_Stop"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical"
android:padding="@dimen/Cursor_col_paddingleft"
android:src="@drawable/imageboutons_stop" />
</RelativeLayout>
正如您在SimpleAudioRecorder类中看到的,我尝试使用isInEditMode()。但首先,它不会改变任何东西,其次我不确定这在下面的情况下是否真的有用。但我可能错了,也许我没有正确使用它。
我认为问题在于SimpleAudioRecorder继承了SimpleAudioPlayer。必须有一些我忘记做的事情...而且由你来帮助我^ _ ^。
提前谢谢!
编辑:
解决方案过于简单......我是如此愚蠢,以至于我忘了删除&#34;摘要&#34; SimpleAudioRecorder类中的关键字。 -_-
感谢njzk2