仅播放通知默认声音(Android)

时间:2012-04-26 14:01:29

标签: android audio notifications

如何才能播放通知声音(不触发状态栏通知)?我想要通知默认声音,并将其作为通知声音播放。是否可以使用MediaPlayer实现它?

3 个答案:

答案 0 :(得分:28)

Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

MediaPlayer mediaPlayer = new MediaPlayer();

try {
      mediaPlayer.setDataSource(context, defaultRingtoneUri);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
      mediaPlayer.prepare();
      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

         @Override
         public void onCompletion(MediaPlayer mp)
         {
            mp.release();
         }
      });
  mediaPlayer.start();
} catch (IllegalArgumentException e) {
 e.printStackTrace();
} catch (SecurityException e) {
 e.printStackTrace();
} catch (IllegalStateException e) {
 e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}

答案 1 :(得分:8)

void addCenteredText(String text, PDFont font, int fontSize, PDPageContentStream content, PDPage page, Point2D.Float offset) throws IOException {
    content.setFont(font, fontSize);
    content.beginText();

    // Rotate the text according to the page orientation
    boolean pageIsLandscape = isLandscape(page);
    Point2D.Float pageCenter = getCenter(page);

    // We use the text's width to place it at the center of the page
    float stringWidth = getStringWidth(text, font, fontSize);
    if (pageIsLandscape) {
        float textX = pageCenter.x - stringWidth / 2F + offset.x;
        float textY = pageCenter.y - offset.y;
        // Swap X and Y due to the rotation
        content.setTextMatrix(Matrix.getRotateInstance(Math.PI / 2, textY, textX));
    } else {
        float textX = pageCenter.x - stringWidth / 2F + offset.x;
        float textY = pageCenter.y + offset.y;
        content.setTextMatrix(Matrix.getTranslateInstance(textX, textY));
    }

    content.showText(text);
    content.endText();
}

boolean isLandscape(PDPage page) {
    int rotation = page.getRotation();
    final boolean isLandscape;
    if (rotation == 90 || rotation == 270) {
        isLandscape = true;
    } else if (rotation == 0 || rotation == 360 || rotation == 180) {
        isLandscape = false;
    } else {
        LOG.warn("Can only handle pages that are rotated in 90 degree steps. This page is rotated {} degrees. Will treat the page as in portrait format", rotation);
        isLandscape = false;
    }
    return isLandscape;
}

Point2D.Float getCenter(PDPage page) {
    PDRectangle pageSize = page.getMediaBox();
    boolean rotated = isRotated(page);
    float pageWidth = rotated ? pageSize.getHeight() : pageSize.getWidth();
    float pageHeight = rotated ? pageSize.getWidth() : pageSize.getHeight();

    return new Point2D.Float(pageWidth / 2F, pageHeight / 2F);
}

float getStringWidth(String text, PDFont font, int fontSize) throws IOException {
    return font.getStringWidth(text) * fontSize / 1000F;
}

答案 2 :(得分:0)

您可以尝试

Private Sub cn()
    Dim totalcn As Double
    Dim cellDoub As Double
    For Each rw As DataGridViewRow In Form1.DataGridView2.Rows
        If rw.Cells(2).Value.ToString = ComboBox1.Text Then
            If Double.TryParse(rw.Cells(1).Value.ToString, cellDoub) Then
                totalcn += cellDoub
            End If
        End If
    Next
    Label11.Text = totalcn.ToString
End Sub